#include "config.h"
#include "at_com.h"

extern u8 TxBuffer1[255];

int at_send_CWJAP(const char *ssid, const char *pwd)
{
  printf("call %s\r\n", __FUNCTION__);
  sprintf((char *)TxBuffer1, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pwd);
  USART_DMACmd(USART3, USART_DMAReq_Tx, DISABLE);
  DMA_SetCurrDataCounter(USART_TX_CH, strlen(TxBuffer1));
  USART_TX_CH->MADDR = (uint32_t)&TxBuffer1[0];
  USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
  while (DMA_GetFlagStatus(DMA1_FLAG_TC2) == RESET) /* Wait until USART3 TX DMA1 Transfer Complete */
    ;
  return 0;
}

int at_send_MQTTUSERCFG(const char* dev_name)
{
  printf("call %s\r\n", __FUNCTION__);
  sprintf((char *)TxBuffer1, "AT+MQTTUSERCFG=0,1,\"%s\",\"\",\"\",0,0,\"\"\r\n", dev_name);
  USART_DMACmd(USART3, USART_DMAReq_Tx, DISABLE);
  DMA_SetCurrDataCounter(USART_TX_CH, strlen(TxBuffer1));
  USART_TX_CH->MADDR = (uint32_t)&TxBuffer1[0];
  USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
  while (DMA_GetFlagStatus(DMA1_FLAG_TC2) == RESET) /* Wait until USART3 TX DMA1 Transfer Complete */
    ;
  return 0;
}

int at_send_MQTTCONN(const char *ip_str)
{
  printf("call %s\r\n", __FUNCTION__);
  sprintf((char *)TxBuffer1, "AT+MQTTCONN=0,\"%s\",1883,1\r\n", ip_str);
  USART_DMACmd(USART3, USART_DMAReq_Tx, DISABLE);
  DMA_SetCurrDataCounter(USART_TX_CH, strlen(TxBuffer1));
  USART_TX_CH->MADDR = (uint32_t)&TxBuffer1[0];
  USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
  while (DMA_GetFlagStatus(DMA1_FLAG_TC2) == RESET) /* Wait until USART3 TX DMA1 Transfer Complete */
    ;
  return 0;
}

int at_send_MQTTSUB(const char *topic) 
{ 
  printf("call %s\r\n", __FUNCTION__);
  sprintf((char *)TxBuffer1, "AT+MQTTSUB=0,\"%s\",0\r\n", topic);
  USART_DMACmd(USART3, USART_DMAReq_Tx, DISABLE);
  DMA_SetCurrDataCounter(USART_TX_CH, strlen(TxBuffer1));
  USART_TX_CH->MADDR = (uint32_t)&TxBuffer1[0];
  USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
  while (DMA_GetFlagStatus(DMA1_FLAG_TC2) == RESET) /* Wait until USART3 TX DMA1 Transfer Complete */
    ;
  return 0;
}
